package exporter

import (
	"emc_vnx_exporter/utils"
	"github.com/prometheus/client_golang/prometheus"
	"io/ioutil"
	"log"
	"os/exec"
	"strings"
	"time"
)

type SP struct {
	Name                           string
	StorageProcessor               string
	StorageProcessorIPAddress      string
	StorageProcessorSubnetMask     string
	StorageProcessorGatewayAddress string
	StorageProcessorIPv6Mode       string
	SerialNumberForTheSP           string
	StatisticsLogging              string
	SystemFaultLED                 string
}

var (
	sp = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Name: "vnx_get_all_sp",
			Help: "Get All SP Info",
		},
		[]string{"Name", "StorageProcessor", "StorageProcessorIPAddress", "StorageProcessorSubnetMask", "StorageProcessorGatewayAddress", "StorageProcessorIPv6Mode", "SerialNumberForTheSP", "StatisticsLogging", "SystemFaultLED"},
	)
)

func SPGauge() *prometheus.GaugeVec {
	return sp
}

func GetSPListMetrics(spIp string, user string, password string) {
	for {
		cmd := exec.Command("bash", "-c", "/opt/Navisphere/bin/naviseccli -h "+spIp+" -user "+user+" -password "+password+" -Scope 0 getall -sp | awk 'n==1{print} $0~/--------------/{n=1}'")
		stdout, _ := cmd.StdoutPipe()
		defer stdout.Close()
		if err := cmd.Start(); err != nil {
			log.Fatalf("cmd.Start: %v", err)
		}
		result, _ := ioutil.ReadAll(stdout)
		resData := string(result)

		spList := strings.Split(resData, "\n\n")
		for _, spString := range spList {
			spItems := strings.Split(spString, "\n")
			data := new(SP)
			for _, spItem := range spItems {
				if spItem == "" {
					continue
				}
				value := spItem[strings.Index(spItem, ":")+1 : strings.Count(spItem, "")-1]
				value = strings.Replace(value, "  ", "", -1)
				if strings.Contains(spItem, "Storage Processor:") {
					data.Name = value
					data.StorageProcessor = value
				} else if strings.Contains(spItem, "Storage Processor IP Address") {
					data.StorageProcessorIPAddress = value
				} else if strings.Contains(spItem, "Storage Processor Subnet Mask") {
					data.StorageProcessorSubnetMask = value
				} else if strings.Contains(spItem, "Storage Processor Gateway Address") {
					data.StorageProcessorGatewayAddress = value
				} else if strings.Contains(spItem, "Storage Processor IPv6 Mode") {
					data.StorageProcessorIPv6Mode = value
				} else if strings.Contains(spItem, "Serial Number For The SP") {
					data.SerialNumberForTheSP = value
				} else if strings.Contains(spItem, "Statistics Logging") {
					data.StatisticsLogging = value
				} else if strings.Contains(spItem, "System Fault LED") {
					data.SystemFaultLED = value
				}
			}
			if data.Name != "" {
				sp.WithLabelValues(
					data.Name,
					data.StorageProcessor,
					data.StorageProcessorIPAddress,
					data.StorageProcessorSubnetMask,
					data.StorageProcessorGatewayAddress,
					data.StorageProcessorIPv6Mode,
					data.SerialNumberForTheSP,
					data.StatisticsLogging,
					data.SystemFaultLED,
				).Set(utils.SPFaultLedToFloat(data.SystemFaultLED))
			}
		}
		time.Sleep(60 * time.Second)
	}
}
